import modelExtend from 'dva-model-extend'
import { routerRedux } from 'dva/router'
import { pageModel } from '@/common/model/BaseModel'
import { message } from 'antd'
import { getImgPrefix } from '../utils/utils'
import {
  getList,
  getPropSelectList,
  delProperty,
  getPropDetail,
  saveProperty,
  setPropLife,
  exportList,
  propImages,
} from '../services/property'

export default modelExtend(pageModel, {
  namespace: 'property',
  state: {
    currentItem: {},
    // propList table select
    selectedRowKeys: [],
    // propLife table select
    selectedRowKeys_PropLife: [],
    // searchForm values
    formValues: {},
    //
    // 资产分类数据
    property_class: [],
    // 资产类型
    property_type: [],
    // 所在位置
    location_seat: [],
    // 资产状态
    property_status: [],
    //
    showView: false,
    propImages: [],
  },
  effects: {
    // 校验路径唯一性
    // *checkUnique({ payload }, { call }) {
    //   return yield call(checkUnique, payload)
    // },
    *fetchList({ payload }, { call, put }) {
      console.log('fetchList')
      const res = yield call(getList, payload || {})

      yield put({
        type: 'updateState',
        payload: {
          list: res.data.records,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: res.data.total,
            current: res.data.current,
            pageSize: res.data.size,
          },
          formValues: payload,
        },
      })
    },

    *fetchSelectPropData({ payload }, { call, put }) {
      const res = yield call(getPropSelectList, payload)
      yield put({
        type: 'updateState',
        payload: {
          [payload.type]: res.data,
        },
      })
    },

    *remove({ payload }, { call, put, select }) {
      const res = yield call(delProperty, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            selectedRowKeys: [],
          },
        })
        const formValues = yield select(state => state.formValues)
        yield put({
          type: 'fetchList',
          payload: {
            current: 1,
            ...formValues,
            type: 1,
          },
        })
      }
    },

    *edit({ payload }, { call, put }) {
      //
      const res = yield call(getPropDetail, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            currentItem: res.data,
          },
        })
        yield put(
          routerRedux.push({
            pathname: '/property/update',
          }),
        )
      }
    },

    *save({ payload }, { call, put }) {
      const res = yield call(saveProperty, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            currentItem: {},
          },
        })
        yield put(
          routerRedux.push({
            pathname: '/property/list',
          }),
        )
      }
    },

    *setLife({ payload, callback }, { call, put }) {
      const res = yield call(setPropLife, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            selectedRowKeys_PropLife: [],
          },
        })
        if (callback && typeof callback === 'function') {
          callback()
        }
      }
    },

    *exportList({ payload }, { call }) {
      yield call(exportList, payload)
    },

    *images({ payload }, { call, put }) {
      const res = yield call(propImages, payload)
      if (res && res.success) {
        if (res.data.length < 1) {
          message.info('暂无图片信息')
        } else {
          yield put({
            type: 'updateState',
            payload: {
              showView: true,
              propImages: res.data
                .map(item => {
                  if (item.itemType === '2') {
                    const path = JSON.parse(item.path)
                    return {
                      src: `${getImgPrefix()}${path.sourceName}`,
                    }
                  }
                  return null
                })
                .filter(item => item || false),
            },
          })
        }
      }
    },
  },
})
